#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "globals.h"
#include "keyboard.h"
#include "system_timer.c"
#include "scheduler.c"
#include "system_call_library.h"
#include "cpu.c"
#include "io_device.c"

/*##################################################################################################
 # Variables
 ##################################################################################################*/

int simulator_countdown;

/**
The parameters passed to the program will be 
the number of the processes (a pre-assigned menu of processes - see below)
 a user interface process (e.g. one waiting for keyboard input)
 a calculating process (possible background job)
 two processes that require inter-process communications, such as a producer-consumer pair
the order to initialize and start them
the number of total time ticks to run the simulation.
**/

int main(int argc, char** args) {


	// Test timer functionality
//	timer_tick();

	// Test Random Number
//	rand_num();

//Funtionality starts here.
simulator_countdown = 15; //no to ticks the scheduler would run; needs to be initialized to a command line argument.

//Setup Scheduler
create_ready_queue();

//Setup IO Devices
set_up_disk();
set_up_video();

//Calculating process
ProcessStr *calc_process = malloc(sizeof(ProcessStr)); //Calculating process
strcpy(calc_process->name, "cp1");
calc_process->no_steps = rand_num();
calc_process->no_requests = 0;//right now this process does not make any requests
printf("Process cp1 created. This process , does some basic computing.\n");
add_process_to_readyqeueue(calc_process);
//Process which makes video IO request
ProcessStr *video_process = malloc(sizeof(ProcessStr)); 
strcpy(video_process->name, "cp2");
video_process->no_steps = rand_num();
video_process->no_requests = 1;
video_process->requests = malloc(sizeof(int));
video_process->req_type =  malloc(sizeof(char));
video_process->requests[0] = (video_process->no_steps)/2;
video_process->req_type[0] = 'v'; //request is for video io device.
printf("Process cp2 created which will make an video io request.\n");
add_process_to_readyqeueue(video_process);

//Process which makes disk IO request
ProcessStr *disk_process = malloc(sizeof(ProcessStr));
strcpy(disk_process->name, "cp3");
disk_process->no_steps = rand_num();
disk_process->no_requests = 1;
disk_process->requests = malloc(sizeof(int));
disk_process->req_type =  malloc(sizeof(char));
disk_process->requests[0] = (disk_process->no_steps)/2;
disk_process->req_type[0] = 'd'; //request is for disk io device.
printf("Process cp2 created which will make an disk io request.\n");
add_process_to_readyqeueue(disk_process);

scheduler_start(); //gets the next process running

while(simulator_countdown != 0)
{
	if(ready_queue->head->next->state == 0)
	{
	   printf("Process %s is currently running\n", ready_queue->head->next->pid->name);
	}
	cpu();
	simulator_countdown--;

}
if(simulator_countdown == 0)
{
	puts("The simulation is done. Exiting now.\n");
}
	return 0;
}
